<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of WeatherPipeline
 *
 * @author Michal
 */
class ADS_ContentPipeline/* implements ADS_Pipeline*/ {
    
    private $services = array();
    private $formats  = array(
        'json' => null,
        'xml'  => null
    );
    
    public function registerService($service, $params, $format = 'json'){
        if(!in_array($format, array_keys($this->formats)) || !is_string($service) || !is_array($params)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        $this->services[$service]['url']    = $params['url'] . '?' . http_build_query((array)$params['query']);
        $this->services[$service]['format'] = $format;
    }
    
    public function registerFormat($alias, ADS_Format $format){
        $this->formats[$alias] = $format;
    }

    //put your code here
    public function toJSON($service){
        if(!isset ($this->services[$service])){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        $response = file_get_contents($this->services[$service]['url']);
        if($this->services[$service]['format'] == 'json'){
            header('Content-type: application/json'); 
            print $response;
        }
        elseif($this->services[$service]['format'] == 'xml'){
            header('Content-type: application/json'); 
            print json_encode(simplexml_load_string($response));
        }
        elseif(in_array($this->services[$service]['format'], array_keys($this->formats))){
            $reader = $this->formats[$this->services[$service]['format']];
            $reader->parse($response);
            print json_encode((object)$reader->toArray());
        }
    }
}

?>
